package com.example.hotelmanager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.hotelmanager.entity.BookMsg;
import com.example.hotelmanager.entity.CheckIn;
import com.example.hotelmanager.entity.Cost;
import com.example.hotelmanager.entity.CostType;
import com.example.hotelmanager.mapper.CheckInMapper;
import com.example.hotelmanager.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author :chenjie
 * @date :Created 2022/11/27 22:45
 */
@Service
public class CheckInServiceImpl extends ServiceImpl<CheckInMapper, CheckIn> implements CheckInService {
    @Autowired
    private BookMsgService bookMsgService;
    @Autowired
    private CostTypeService costTypeService;
    @Autowired
    private CostService costService;

    @Override
    public List<CheckIn> getValidCheckIns(Timestamp fromTime, Timestamp toTime) {
        return this.baseMapper.getValidCheckIns(fromTime,toTime);
    }

    @Override
    public List<CheckIn> getValidCheckIns1(Timestamp fromTimeT, Timestamp toTimeT) {
        return this.baseMapper.getValidCheckIns1(fromTimeT,toTimeT);
    }

    @Override
    public Map<String, Object> bookCheckIn(int bookMsgId) {
        Map<String,Object> map=new HashMap<>();
        BookMsg bookMsg = bookMsgService.getById(bookMsgId);
        LambdaQueryWrapper<CheckIn> queryWrapper3=new LambdaQueryWrapper<>();
        queryWrapper3.eq(CheckIn::getRoomId,bookMsg.getResultRoom());
        List<CheckIn> checkIns = this.baseMapper.selectList(queryWrapper3);
        if(checkIns.size()>0){//无法重复入住
            return map;
        }
        CheckIn checkIn = new CheckIn(0, bookMsg.getGuestIdCard(), bookMsg.getResultRoom(), bookMsg.getFromTime(), bookMsg.getToTime(), 1);
        bookMsg.setState(11);
        checkIn.setState(1);
        String costName1 = bookMsg.getResultRoom() + "房间定金";
        String costName2 = bookMsg.getResultRoom() + "房间租金";
        LambdaQueryWrapper<CostType> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(CostType::getName,costName1);
        LambdaQueryWrapper<CostType> queryWrapper2=new LambdaQueryWrapper<>();
        queryWrapper2.eq(CostType::getName,costName2);
        CostType costTypes1 = costTypeService.getOne(queryWrapper);
        CostType costTypes2 = costTypeService.getOne(queryWrapper2);
        Cost cost1 = new Cost(0, costTypes1.getId(), bookMsg.getResultRoom(), 1, 0);
        costService.saveOrUpdate(cost1);
        int costNum = (int) (bookMsg.getToTime().getTime() - bookMsg.getFromTime().getTime()) / 1000 / 60 / 60 / 24;
        cost1 = new Cost(0, costTypes2.getId(), bookMsg.getResultRoom(), costNum, 1);
        costService.saveOrUpdate(cost1);
        map.put("checkIn",checkIn);
        map.put("bookMsg",bookMsg);
        return map;

    }


}
